<?php
namespace plugins\com_message\controller; 
use cmf\controller\PluginAdminBaseController;//引入此类
use think\Db;
use plugins\com_message\model\SendMessage;
use plugins\com_message\model\ComMessagePoolModel;

class AdminIndexController extends PluginAdminBaseController
{

    private  $pageNum = "20";
	/**
	 * 首页
	 */
    public function index()
    {
        $data = $this->request->param();
        $where = 1;
        if( !empty($data['status']) ) $where .= ' and status ='.$data [ 'status' ];
        if( !empty($data['type']) ) $where .= " and type like '%".$data['type']."%'";
        if( !empty($data['mobile']) ) $where .= " and mobile like '%".$data['mobile']."%'";

        $sear['pagesize'] = isset( $data['pagesize'] ) ? $data [ 'pagesize' ] : $this->pageNum;
        $sear['type'] = isset( $data['type'] ) ? $data [ 'type' ] : '';
        $sear['mobile'] = isset( $data['mobile'] ) ? $data [ 'mobile' ] : '';
        $sear['status'] = isset( $data['status'] ) ? $data [ 'status' ] : '';

        $list = db('mail')->where($where)->paginates($sear['pagesize'] , false ,['query'=>$sear])->each(function ($item){
            $item['time'] = date('Y-m-d H:i:s',$item['time']);
            $name = '-';
            if($item['userid'] == 0){
                $name = '管理员';
            }
            $item['username'] = $name;
            return $item;
        });

        $list->appends($data);
        $this->assign('list', $list);
        $this->assign('sear',$sear);
        $this->assign('page', $list->render());
        return $this->fetch();
    }

    /**
     * 站内信详情
     */
    public function encyInfo()
    {
        $param = $this->request->param();
        $info = db('mail')->where('id',$param['id'])->find();
        $full_image = '';
        if(!empty($info['image'])){
            $full_image = ZY_APP_PATH.$info['image'];
        }
        $info['full_image'] = $full_image;
        if(!empty($info['content'])){
            $info['content'] = htmlspecialchars_decode($info['content']);
        }
        return api_json_echo( true , '获取成功' , $info , 200);

    }

    /**
     * 删除站内信
     */
    public function delEncy()
    {
        $data = $this->request->param();
        if (empty($data['id'])) {
            return $this->error('请先选择要删除的列表！');
        }
        $res = Db::name('mail')->where('id', $data['id'])->delete();
        if($res){
            return $this->success('删除成功');
        }
        return $this->error('删除失败');
    }

    /**
     * 站内信添加页面
     */
    public function addPage()
    {
        return $this->fetch();
    }

    /**
     * 站内信编辑页面
     */
    public function editPage()
    {
        $param = $this->request->param();
        $this->assign('id', $param['id']);
        return $this->fetch();
    }

    /**
     * 添加百科
     */
    public function encyAdd()
    {
        $param = $this->request->param();
        $add['userid'] = 0;
        $add['time'] = time();
        $add['title'] = $param['title'];
        $add['content'] = $param['content'];
        $add['image'] = $param['image'];
        $add['category'] = $param['category'];

        $return = db('mail')->insert($add);
        if ($return) {
            return api_json_echo(true, '添加成功！', null, 200);
        } else {
            return api_json_echo(false, '添加失败！', null, 101);
        }
    }

    /**
     * 编辑站内信
     */
    public function encyEdit()
    {
        $param = $this->request->param();
        $add['userid'] = 0;
        $add['time'] = time();
        $add['title'] = $param['title'];
        $add['content'] = $param['content'];
        $add['image'] = $param['image'];

        $return = db('mail')->where('id',$param['id'])->update($add);
        if ($return) {
            return api_json_echo(true, '修改成功！', null, 200);
        } else {
            return api_json_echo(false, '修改失败！', null, 101);
        }
    }

}

